package com.exit66.jukebox.data;

/**
 * @author andyb
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
import java.sql.SQLException;

public class AlbumCollection extends BVDatabaseCollection {
        
    public AlbumCollection() {
        
        _currentRecord = new Album();
        
    }
    
    
    public void list(int start, int count) {
        
        processList("", start, count);
        
    }
    
    public void list(String albumNameSearch, int start, int count) {
        
        processList("WHERE album_name LIKE " + Qts(albumNameSearch), start, count);
        
    }
    
    public void listByArtist(int artistID, int start, int count) {
        
        processList(", artistalbumlink WHERE artistalbumlink.album_id = album.album_id AND artistalbumlink.artist_id = " + artistID, start, count);
        
    }
    
    public void listByCompArtist(int artistID, int start, int count) {
        
        processList(" WHERE multiple_artist_id = " + artistID, start, count);
        
    }
    
    private void processList(String whereClause, int start, int count) {
        
        if (openConnection()) {
            String sql;
        	if (count == 0) 
        		sql = "SELECT ";
        	else
        		sql = "SELECT LIMIT " + start + " " + count;
        	
            _rs = retrieveData(sql + " album.* FROM album " + whereClause + " ORDER BY album_name");
            //_rs = retrieveData("SELECT album.*, 0 as artist_count FROM album " + whereClause + " ORDER BY album_name");
            try {
                
                if (_rs.next()) {
                    _eof = false;
                } else {
                    _eof = true;
                }
                
            } catch (SQLException e) {
                
                _eof = true;
                
            }
            
        }
        
    }
    
    public void listCompilations() {
        
        String sql = "SELECT album.* FROM album WHERE (multiple_artist_id != -1 AND multiple_artist_id != null) ORDER BY album_name ";
        
        if (openConnection()) {
            
            _rs = retrieveData(sql);
            try {
                
                if (_rs.next()) {
                    _eof = false;
                } else {
                    _eof = true;
                }
                
            } catch (SQLException e) {
                
                _eof = true;
                
            }
            
        }
        
    }
    
    public void listPossibleCompilations() {
        
        updateAlbumArtistCount();
        
        String sql = "SELECT album.* FROM album WHERE artist_count > 1 " +
                "AND (multiple_artist_id = -1 OR multiple_artist_id = null)  ORDER BY album_name";
        
        if (openConnection()) {
            
            _rs = retrieveData(sql);
            try {
                
                if (_rs.next()) {
                    _eof = false;
                } else {
                    _eof = true;
                }
                
            } catch (SQLException e) {
                
                _eof = true;
                
            }
            
        }
        
        
    }
    
    protected void loadCurrent() {
        
        try {
            
            ((Album)_currentRecord).setAlbumID(_rs.getInt("album_id"));
            ((Album)_currentRecord).setAlbumName(_rs.getString("album_name"));
            try {
                ((Album)_currentRecord).setArtistCount(_rs.getInt("artist_count"));
            }
            catch (Exception e) {
                ((Album)_currentRecord).setArtistCount(0);
            }
            
        } catch (SQLException e) {};
        
    }
    
    public void updateAlbumArtistCount() {
        
        String sql = "UPDATE album SET artist_count = (SELECT artist_count FROM vw_albumartistcount WHERE album.album_id = vw_albumartistcount.album_id)";
        
        executeStatement(sql);
                
    }
    
}
